<?php
/**
 * SiteController class.
 * SiteController is the controller for business portal.
 * 
 * It used for all functions of login, language, singup in beta version phase (after 2012.09.01).
 * 
 * Yawii Business 版权所有 @ 2012.
 */
class SiteController extends Controller
{
	public $layout = '//layouts/layout-site';
	
	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$this->redirect(Yii::app()->createUrl('/intro'));
	}
	
	public function actionPrivacy()
	{
		$this->render('privacy');
	}
	public function actionTerms()
	{
		$this->render('terms');
	}

	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
		$error = Yii::app()->errorHandler->error;
	    if ($error) {
			//send email to admin
			BUtils::EmailAdmin('Error Page for merchant ' . Yii::app()->user->id, print_r($error, 1));
			
	    	if (Yii::app()->request->isAjaxRequest)
	    		echo $error['message'];
	    	else {
				if (Yii::app()->user->isGuest) {
					$model = new FeedbackForm();
					if (isset($_POST['ajax']) && $_POST['ajax'] === 'feedback-form') {
						echo CActiveForm::validate($model);
						Yii::app()->end();
					}
					if (isset($_POST['FeedbackForm'])) {
						$model->attributes = $_POST['FeedbackForm'];
						$model->id_merchant = Yii::app()->user->id;
						if ($model->save()) {				
							//send email to business@yamii.fi
							$subject = 'Feedback from ' . Yii::app()->user->getState('name');
							BUtils::SendEmailIn($subject, $model->email, $model->content);

							Yii::app()->user->setFlash('success', BUtils::T('feedback_success_msg'));
							$this->refresh();
						}
					}
					$this->render('error', array('model'=>$model, 'error'=>$error));
				} else {
					$this->layout = '//layouts/main';
					$this->render('error', array('error'=>$error));
				}
			}
	    }
	}

	/**
	 * Displays the contact page
	 */
	public function actionContact()
	{
		$model = new ContactForm;
		if(isset($_POST['ContactForm']))
		{
			$model->attributes=$_POST['ContactForm'];
			if($model->validate())
			{
				$headers="From: {$model->email}\r\nReply-To: {$model->email}";
				mail(Yii::app()->params['adminEmail'],$model->subject,$model->body,$headers);
				Yii::app()->user->setFlash('contact','Thank you for contacting us. We will respond to you as soon as possible.');
				$this->refresh();
			}
		}
		$this->render('contact',array('model'=>$model));
	}
	
	/**
	 * Language options
	 */
	public function actionLanguage() {
		$ref = Yii::app()->request->getUrlReferrer();
		if (empty($ref)) $url = Yii::app()->homeUrl;
		else $url = $ref;
		
		BUtils::SetLanguage($_GET['lang']);
		$this->redirect($url);
	}
	
	/**
	 * Push client message
	 */
	public function actionPush() {
		BUtils::Logging('push', $_REQUEST);
	}
}